const KoaBouncer = require('koa-bouncer')
const { ADDVIP, GETVIP, SEARCHVIP ,DELETEVIP,UPDATEVIP,GETTODAYBORN,GETTODAYEXPIRES,GETTODAYADDVIP,GETTHIRTYNOTCONSUM,
    GETVIPRANK,ADDVIPRANK,UPDATEVIPRANK,DELVIPRANK,SEARCHVIPRANK,UPDATEVIPSCORE} = require('../service/vip.service')
module.exports = {
    // 添加会员
    addVip: async (ctx, next) => {
        // 判断客户端的合法性
        try {
            // 会员卡号
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            // 会员名字
            ctx.validateBody('vipName')
                .required('vipnName必须得传值')
                .isString()
                .trim()
                .isLength(2, 8, "用户名必须是2—8位")
            ctx.validateBody('sex')
                .required('sex必须得传值')
                .isString()
                .trim()
                .isLength(1, 2, "sex必须是2—8位")
            ctx.validateBody('vipPhone')
                .required('vipPhone必须得传值')
                .isString()
                .trim()
                .isLength(11, 11, "vipPhone必须是11位")
            ctx.validateBody('vipRank')
                .required('vipRank必须得传值')
                .trim()
            ctx.validateBody('money')
                .required('money必须得传值')
                .trim()
            ctx.validateBody('score')
                .required('score必须得传值')
                .trim()
            ctx.validateBody('openStaff')
                .required('openStaff必须得传值')
                .trim()
            ctx.validateBody('openMoney')
                .required('openMoney必须得传值')
                .trim()
            
            ctx.validateBody('expires')
                .required('expires必须得传值')
                .trim()
            ctx.validateBody('openTime')
                .required('openTime必须得传值')
                .trim()
            ctx.validateBody('born')
                .required('born必须得传值')
                .trim()
            ctx.validateBody('payMethod')
                .required('payMethod必须得传值')
                .trim()
                .isLength(1, 6, "payMethod必须是1-6位")
            ctx.validateBody('openStaff')
                .required('openStaff必须得传值')
                .trim()
            ctx.validateBody('belongToShop')
                .required('belongToShop必须得传值')
            // 验证通过
            await ADDVIP(ctx)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
    // 获取vip
    getVip: async (ctx, next) => {
        
        await GETVIP(ctx, next)
    },
    // 搜索会员
    searchVip: async (ctx, next) => {
        // 判断客户端的合法性
        try {
            // 关键字
            ctx.validateBody('keyWord')
                .required('keyWord必须得传值')
                .isString()
                .trim()
            ctx.validateBody('belongToShop')
                .required('BelongToShop必须得传值')
            // 验证通过
            await SEARCHVIP(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
       
    },
    // 禁用会员
    deleteVip: async (ctx, next) => {
        try {
            // 会员卡号
            ctx.validateBody('id')
                .required('id必须得传值')
            // 验证通过
            await DELETEVIP(ctx, next)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
       
    },
    // 修改会员信息
    updatevip: async (ctx, next) => {
        // 判断客户端的合法性
        try {
            // 会员卡号
            ctx.validateBody('id')
                .required('id必须得传值')
                .isString()
                .trim()
            // 会员名字
            ctx.validateBody('vipName')
                .required('vipnName必须得传值')
                .isString()
                .trim()
                .isLength(2, 8, "用户名必须是2—8位")
            ctx.validateBody('sex')
                .required('sex必须得传值')
                .isString()
                .trim()
                .isLength(1, 2, "sex必须是2—8位")
            ctx.validateBody('vipPhone')
                .required('vipPhone必须得传值')
                .isString()
                .trim()
                .isLength(11, 11, "vipPhone必须是11位")
            ctx.validateBody('vipRank')
                .required('vipRank必须得传值')
                .trim()
            ctx.validateBody('money')
                .required('money必须得传值')
                .trim()
            ctx.validateBody('score')
                .required('score必须得传值')
                .trim()
            ctx.validateBody('openStaff')
                .required('openStaff必须得传值')
                .trim()
           
            
            ctx.validateBody('expires')
                .required('expires必须得传值')
                .trim()
            
            ctx.validateBody('openStaff')
                .required('openStaff必须得传值')
                .trim()
      
            // 验证通过
            await UPDATEVIP(ctx)
        } catch (error) {
            if (error instanceof KoaBouncer.ValidationError) {
                ctx.body = {
                    code: 500,
                    msg: error.message
                }
                return
            } else {
                throw error;
            }
        }
    },
    // 今天过生
    getTodayBorn: async (ctx, next) => {
        await GETTODAYBORN(ctx, next)
    },
    // 今天过期
    getTodayExpires: async (ctx, next) => {
        await GETTODAYEXPIRES(ctx, next)
    },
    // 今天新增会员
    getTodayAddVip: async (ctx, next) => {
        await GETTODAYADDVIP(ctx, next)
    },
    // 本月未消费
    getThirtyNotConsum: async (ctx, next) => {
        await GETTHIRTYNOTCONSUM(ctx, next)
    },
    getVipRank: async (ctx, next) => {
        await GETVIPRANK(ctx, next)
    },
    addVipRank: async (ctx, next) => {
        await ADDVIPRANK(ctx, next)
    },
    updateVipRank: async (ctx, next) => {
        await UPDATEVIPRANK(ctx, next)
    },
    delVipRank: async (ctx, next) => {
        await DELVIPRANK(ctx, next)
    },
    searchVipRank: async (ctx, next) => {
        await SEARCHVIPRANK(ctx, next)
    },
    updateVipScore: async (ctx, next) => {
        await UPDATEVIPSCORE(ctx, next)
    },
}